#include <gtest/gtest.h>

#include <memory>
#include <thread>

#include "KVCommand.h"
#include "KVMiddleware.h"
#include "KVStateGRpc.h"
#include "KVStateMachine.h"
#include "raft_core/RaftCoreGrpc.h"


TEST(KV_RAFT, BASICTEST) {
    auto kv_command_middleware = std::make_shared<KVCommand>();
    auto kv_middleware = std::make_unique<KVMiddleware>(kv_command_middleware);

    auto kv_state_machine = std::make_shared<KVStateMachine>();
    auto kv_state_grpc = std::make_unique<KVStateGrpc>();
    std::thread t1([&] {
        kv_state_grpc->start(kv_state_machine);
    });
    t1.detach();

    auto kv_command_core = std::make_shared<KVCommand>();
    auto raft_core_grpc = std::make_shared<raft::core::RaftCoreGrpc>();
    std::thread t2([&] {
        raft_core_grpc->start(kv_state_machine, kv_command_core);
    });
    t2.detach();

    std::this_thread::sleep_for(std::chrono::seconds(5));

    std::string test = "INSERT 1 test1";
    auto result = kv_middleware->recv_msg(test);
    std::cout << "INSERT 1 test1 result: " << result << "\n";

    std::this_thread::sleep_for(std::chrono::seconds(10));

    std::string test1 = "SEARCH 1 ";
    result = kv_middleware->recv_msg(test1);
    std::cout << "SEARCH 1 result: " << result << "\n";

    std::this_thread::sleep_for(std::chrono::seconds(1000));
}